package com.galleryviewpager.g_error;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class ErrorView extends View {

    private int mWidth, mHeight;
    private Paint paint = new Paint();
    private RectF rectF = new RectF();

    private int lineLengthProgress =0;

    ScheduledFuture future;

    private int mProgress = 0;

    public ErrorView(Context context) {
        this(context, null);
    }

    public ErrorView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ErrorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {

        paint.setStrokeWidth(10f);
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);

        changeProgress();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
            mWidth = MeasureSpec.getSize(widthMeasureSpec);
        } else {
            mWidth = 400;
        }

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY) {
            mHeight = MeasureSpec.getSize(heightMeasureSpec);
        } else {
            mHeight = 400;
        }
        setMeasuredDimension(mWidth, mHeight);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        rectF.set(10, 10, mWidth - 10, mHeight - 10);
        canvas.drawArc(rectF, 180, 360 * mProgress / 100, false, paint);

        if (mProgress < 104) {
            canvas.drawLine(mWidth / 8 + 10, mHeight / 8 + 10, (int) (mWidth*0.75*lineLengthProgress/100)+10, (int) (mHeight*0.75*lineLengthProgress/100)+10,paint);
        }
    }

    private void changeProgress() {
        ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        future = scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {


                post(new Runnable() {
                    @Override
                    public void run() {

                        if (mProgress == 100) {
                            if (!future.isCancelled()) {
                                future.cancel(true);
                            }
                            return;
                        }
                        mProgress++;
                        lineLengthProgress++;
                        postInvalidate();
                    }
                });

            }
        }, 1000, 200, TimeUnit.MILLISECONDS);
    }

}